package mj.scott.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import mj.scott.pojo.dto.DishDTO;
import mj.scott.pojo.entity.Dish;
import mj.scott.mapper.DishMapper;
import mj.scott.pojo.entity.DishFlavor;
import mj.scott.service.IDishFlavorService;
import mj.scott.service.IDishService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 菜品 服务实现类
 * @author Scott
 * @since 2025-08-15
 */

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements IDishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private IDishFlavorService dishFlavorService;

    @Transactional
    @Override
    public void saveWithFlavor(DishDTO dishDTO) {
        // 保存菜品基本信息
        this.save(dishDTO);
        Long dishId = dishDTO.getId();
        // 保存菜品口味信息
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && !flavors.isEmpty()) {
            flavors.forEach(flavor -> flavor.setDishId(dishId));
            dishFlavorService.saveBatch(flavors);
        }
    }

    @Override
    public DishDTO getByIdWithFlavor(Long id) {
        Dish dish = this.getById(id);
        DishDTO dishDTO = new DishDTO();
        BeanUtils.copyProperties(dish, dishDTO);
        // 查询口味
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId, id);
        List<DishFlavor> flavors = dishFlavorService.list(queryWrapper);
        dishDTO.setFlavors(flavors);
        return dishDTO;
    }

    @Transactional
    @Override
    public void updateWithFlavor(DishDTO dishDTO) {
        // 更新菜品基本信息
        this.updateById(dishDTO);
        Long dishId = dishDTO.getId();
        // 先删除原有口味
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId, dishId);
        dishFlavorService.remove(queryWrapper);
        // 再添加新的口味
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && !flavors.isEmpty()) {
            flavors.forEach(flavor -> flavor.setDishId(dishId));
            dishFlavorService.saveBatch(flavors);
        }
    }

    @Transactional
    @Override
    public void removeWithFlavor(List<Long> ids) {
        // 删除菜品
        this.removeByIds(ids);
        // 删除关联口味
        for (Long id : ids) {
            LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(DishFlavor::getDishId, id);
            dishFlavorService.remove(queryWrapper);
        }
    }

    @Override
    public void updateStatusBatch(Integer status, List<Long> ids) {
        List<Dish> dishList = ids.stream().map(id -> {
            Dish dish = new Dish();
            dish.setId(id);
            dish.setStatus(status);
            return dish;
        }).collect(Collectors.toList());
        this.updateBatchById(dishList);
    }

    @Override
    public Page<DishDTO> getPage(Integer page, Integer pageSize, String name) {
        Page<Dish> dishPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null, Dish::getName, name);
        queryWrapper.orderByDesc(Dish::getId);
        this.page(dishPage, queryWrapper);

        Page<DishDTO> dishDtoPage = new Page<>();
        BeanUtils.copyProperties(dishPage, dishDtoPage, "records");

        List<Dish> dishList = dishPage.getRecords();
        List<DishDTO> dishDtoList = dishList.stream().map(dish -> {
            DishDTO dishDTO = new DishDTO();
            BeanUtils.copyProperties(dish, dishDTO);
            // 查询该菜品的口味信息
            LambdaQueryWrapper<DishFlavor> flavorQueryWrapper = new LambdaQueryWrapper<>();
            flavorQueryWrapper.eq(DishFlavor::getDishId, dish.getId());
            List<DishFlavor> flavors = dishFlavorService.list(flavorQueryWrapper);
            dishDTO.setFlavors(flavors);
            return dishDTO;
        }).collect(Collectors.toList());

        dishDtoPage.setRecords(dishDtoList);
        return dishDtoPage;
    }

    @Override
    public Page<DishDTO> getByCategoryId(Integer page, Integer pageSize, Long categoryId) {
        Page<DishDTO> dishPage = new Page<>(page, pageSize);
        return dishMapper.selectByCategoryId(dishPage, categoryId, null);
    }

}

